/*
 * TextPanel.java
 *
 * 
 */

package it.luigiarpino.alt2jsim.netpanel.flat;

import it.luigiarpino.alt2jsim.core.Grafo;
import it.luigiarpino.alt2jsim.core.ScriviGrafo;
import it.luigiarpino.alt2jsim.utility.TextFieldColor;

import java.awt.*;
import javax.swing.*;

/**
 *
 * @author  Luigi Lorenzo Arpino 534/559
 */
public class TextPanel extends JPanel {
    
    /**
	 * 
	 */
	private static final long serialVersionUID = -934076722170053465L;
	private Grafo grafo;
    private JPanel jPanelNorthPanel;
    private JEditorPane jEditorPaneTesto;
    private JScrollPane jScrollPane;
    private TextFieldColor textFieldColorRitardoLink;
    private TextFieldColor textFieldColorBuffer;
    private TextFieldColor textFieldColorBanda;
    private JButton jButtoAggiorna;
    
    /** Creates a new instance of TextPanel */
    public TextPanel(Grafo grafo_) {
        super();
        try { 
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());     
    	} 
    	catch(Exception e) { }
        try{
            this.grafo=grafo_;
            this.jPanelNorthPanel=new JPanel(new GridLayout(4,1));
            // costruisco il jPanelNorthPanel
            // Prima riga
            JPanel pannello1=new JPanel(new FlowLayout(FlowLayout.LEADING));
            pannello1.add(new JLabel("Maximum delay in seconds of the links"));
            this.textFieldColorRitardoLink=new TextFieldColor(4);
            this.textFieldColorRitardoLink.setText("1");
            pannello1.add(this.textFieldColorRitardoLink);
            //Seconda riga
            JPanel pannello2=new JPanel(new FlowLayout(FlowLayout.LEADING));
            pannello2.add(new JLabel("Buffer size of nodes in Kbytes"));
            this.textFieldColorBuffer=new TextFieldColor(8);
            this.textFieldColorBuffer.setText("1024");
            pannello2.add(this.textFieldColorBuffer);
            //Terza riga
            JPanel pannello3=new JPanel(new FlowLayout(FlowLayout.LEADING));
            pannello3.add(new JLabel("Nodes interface bandwidth in Kbps"));
            this.textFieldColorBanda=new TextFieldColor(8);
            this.textFieldColorBanda.setText("1024");
            pannello3.add(this.textFieldColorBanda);
            //Quarta riga
            this.jButtoAggiorna=new JButton("Show translation");
            this.jButtoAggiorna.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButtonAggiornaClick(evt);
                }
            });
            JPanel pannello4=new JPanel(new FlowLayout(FlowLayout.LEADING));
            pannello4.add(this.jButtoAggiorna);
            // Aggiungo i tre pannelli al jPanelNorthPanel
            this.jPanelNorthPanel.add(pannello1);
            this.jPanelNorthPanel.add(pannello2);
            this.jPanelNorthPanel.add(pannello3);
            this.jPanelNorthPanel.add(pannello4);
            // imposto jEditorPaneTesto
            this.jEditorPaneTesto = new JEditorPane("text/html","");
            this.jScrollPane=new JScrollPane();
            this.jScrollPane.setViewportView(this.jEditorPaneTesto );
            this.setLayout(new BorderLayout());
            this.add(this.jPanelNorthPanel,BorderLayout.NORTH);
            this.add(this.jScrollPane,BorderLayout.CENTER);
        }
        catch(java.lang.Exception e){
            System.out.println("error display TextPanel");
        }
        
    }
    
     private void jButtonAggiornaClick(java.awt.event.ActionEvent evt) { 
         this.jButtoAggiorna.setText("Update");
         Double ritardo;
         Long buffer;
         Long banda;
         try{
             ritardo=new Double(this.textFieldColorRitardoLink.getText());
         }
         catch(java.lang.Exception e){
             JOptionPane.showMessageDialog(null,"Delay not valid!","Error",JOptionPane.ERROR_MESSAGE);
             return;
         }
         try{
             buffer=new Long(this.textFieldColorBuffer.getText()); 
         }
         catch(java.lang.Exception e){
             JOptionPane.showMessageDialog(null,"Buffer dimension not valid!","Error",JOptionPane.ERROR_MESSAGE);
             return;
         }
         try{
            banda=new Long(this.textFieldColorBanda.getText());
         }
         catch(java.lang.Exception e){
             JOptionPane.showMessageDialog(null,"Bandwidth not valid!","Error",JOptionPane.ERROR_MESSAGE);
             return;
         }
         ScriviGrafo write=new ScriviGrafo(this.grafo);
        
         this.jEditorPaneTesto = new JEditorPane("text/html",write.getScenarioHTML(ritardo.doubleValue() , buffer.longValue(),banda.longValue(),true,true));
         this.jEditorPaneTesto.setMargin(new Insets(10,10,10,10));
         this.jEditorPaneTesto.setFont(new Font("Monospaced",Font.PLAIN,14));
         this.jScrollPane.setViewportView(this.jEditorPaneTesto );
        
         //JTextArea text=new JTextArea(write.getScenarioTxt(ritardo.doubleValue() , buffer.longValue(),banda.longValue(),true,true));
         //text.setFont(new Font("Monospaced",Font.PLAIN,14));
         //text.setMargin(new Insets(10,10,10,10));
         //this.jScrollPane.setViewportView(text);
     }
   
     
    public static void main(String[] args) {
        
    }
    
}
